package mitm;

import java.io.*;
import java.net.*;
import java.security.*;

/**
 * Created with IntelliJ IDEA.
 * User: Paula
 * Date: 3/12/13
 * Time: 12:03 PM
 * A class used to generate the password file and the pk/sk pair for RSA challenge
 */
public class GeneratePasswordFiles
{
    public static void main( String [] args )
    {
        final String passwordFile = "pwdFile.txt";
        final String keyStoreFile = "clientkeystore.jks";

        if(args.length != 1)
        {
            System.err.println("specifify password");
            System.exit(1);
        }

        final String password = args[0];

        final String passwordHash = BCrypt.hashpw(password, BCrypt.gensalt());

        try
        {
            final KeyStore keyStore;

            if (keyStoreFile != null) {
                keyStore = KeyStore.getInstance("jks");
                keyStore.load(new FileInputStream(keyStoreFile), password.toCharArray());


            } else {
                keyStore = null;
            }

            PrivateKey privateKey = (PrivateKey)keyStore.getKey("mykey", password.toCharArray()) ;

            iaik.x509.X509Certificate certificate = new iaik.x509.X509Certificate(keyStore.getCertificate("mykey").getEncoded());
            PublicKey publicKey = certificate.getPublicKey();

            BufferedWriter w = new BufferedWriter(new FileWriter(passwordFile));

            w.write(passwordHash);
            w.newLine();
            w.write(javax.xml.bind.DatatypeConverter.printBase64Binary(publicKey.getEncoded()));

            w.close();




        }
        catch (Exception e) {}

        System.exit(0);
    }
}
